If you want to use a certain mode foo for all files whose names end with the extension .bar, this will do it for you:
(add-to-list 'auto-mode-alist '("\\.bar\\'" . foo-mode))
Alternatively, put this somewhere in the first line of any file you want to edit in the mode foo (in the second line, if the first line begins with ‘#!’):
-*- foo -*-
The variable
interpreter-mode-alist specifies which mode to use
when loading an interpreted script (e.g. shell, python, etc.).
Emacs determines which interpreter you're using by examining the
first line of the script. Use C-h v (or M-x
describe-variable) on interpreter-mode-alist
to learn more.